import { h, ref } from 'vue';
import { NTag } from 'naive-ui';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import {createStorage} from "@/utils/Storage";
import {LATEST_DAY} from "@/store/mutation-types";
const storage = createStorage({ storage: localStorage });
let latestDay = Date.parse(storage.get(LATEST_DAY))

export class State {
  public code = ''; // code
  public name = ''; // name
  public sumBuy = null; // 龙虎榜买入额
  public sumSell = null; // 龙虎榜卖出额
  public netAmount = null; // 龙虎榜净买额
  public buyLhb = null; // 龙虎榜成交额
  public buyMarket = null; // 市场总成交额
  public date = ''; // 上榜日期
  public desc = ''; // 解读
  public reason = ''; // 上榜原因
  public sellSeat = null; // sell_seat
  public buySeat = null; // buy_seat
  public closed = null; // 收盘价格
  public changePercent = null; // 涨跌幅

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

export const rules = {};

export const schemas = ref<FormSchema[]>([
  // {
  //   field: 'code',
  //   component: 'NInput',
  //   label: 'code',
  //   componentProps: {
  //     placeholder: '请输入code',
  //     onUpdateValue: (e: any) => {
  //       console.log(e);
  //     },
  //   },
  // },
  {
    field: 'date',
    component: 'NDatePicker',
    label: '上榜日期',
    defaultValue:latestDay,
    componentProps: {
      placeholder: '请输入上榜日期',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'category',
    component: 'NRadioGroup',
    label: '分类',
    componentProps: {
      options: [
        {
          label: '全部',
          value: 'all',
        },
        {
          label: '上证主板',
          value: 'shzb',
        },
        {
          label: '深证主板',
          value: 'szzb',
        },
        {
          label: '创业板',
          value: 'cyb',
        },
        {
          label: '科创版',
          value: 'kcb',
        },
        {
          label: '北交所',
          value: 'bjs',
        },
        {
          label: '可转债',
          value: 'kzz',
        },
      ],
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

export const columns = [
  {
    title: '日期',
    key: 'date',
    width: 100,
  },
  {
    title: '代码',
    key: 'code',
    width: 80,
  },
  {
    title: '名称',
    key: 'name',
    width: 80,
  },
  {
    title: '涨跌幅',
    key: 'changePercent',
    width: 100,
    render: (row: any) => {
      return h(
        NTag,
        {
          type: row.changePercent < 0 ? 'success' : 'error',
        },
        {
          default: () => row.changePercent + '%',
        }
      );
    },
  },
  {
    title: '收盘价格',
    key: 'closed',
    width: 80,

  },
  {
    title: '龙虎榜买入额',
    key: 'sumBuy',
    width: 100,

    render: (row: any) => {
      const num = Number(row.sumBuy);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        if (num / (10000 * 10000) < 0.1) {
          return (num / 10000).toFixed(2) + '万';
        } else {
          return (num / (10000 * 10000)).toFixed(2) + '亿';
        }
      }
    },
  },
  {
    title: '龙虎榜卖出额',
    key: 'sumSell',
    width: 100,

    render: (row: any) => {
      const num = Number(row.sumSell);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        if (num / (10000 * 10000) < 0.1) {
          return (num / 10000).toFixed(2) + '万';
        } else {
          return (num / (10000 * 10000)).toFixed(2) + '亿';
        }
      }
    },
  },
  {
    title: '龙虎榜净买额',
    key: 'netAmount',
    width: 100,

    render: (row: any) => {
      const num = Number(row.netAmount);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        if (num / (10000 * 10000) < 0.1) {
          return (num / 10000).toFixed(2) + '万';
        } else {
          return (num / (10000 * 10000)).toFixed(2) + '亿';
        }
      }
    },
  },
  {
    title: '龙虎榜成交额',
    key: 'buyLhb',
    width: 100,

    render: (row: any) => {
      const num = Number(row.buyLhb);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        if (num / (10000 * 10000) < 0.1) {
          return (num / 10000).toFixed(2) + '万';
        } else {
          return (num / (10000 * 10000)).toFixed(2) + '亿';
        }
      }
    },
  },
  {
    title: '市场总成交额',
    key: 'buyMarket',
    width: 100,

    render: (row: any) => {
      const num = Number(row.buyMarket);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        if (num / (10000 * 10000) < 0.1) {
          return (num / 10000).toFixed(2) + '万';
        } else {
          return (num / (10000 * 10000)).toFixed(2) + '亿';
        }
      }
    },
  },
  {
    title: '解读',
    key: 'desc',
  },
];
